package com.imooc.controller.center;

import com.github.pagehelper.PageHelper;
import com.imooc.BO.center.OrderItemsCommentBO;
import com.imooc.controller.BaseController;
import com.imooc.enums.YesOrNo;
import com.imooc.pojo.OrderItems;
import com.imooc.pojo.Orders;
import com.imooc.service.MyCommentsService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.vo.MyCommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gcq
 * @Create 2022-01-25
 */
@Api(value = "用户中心评价模块", tags = {"用户中心评价模块"})
@RestController
@RequestMapping("mycomments")
public class MyCommentsController extends BaseController {

    @Autowired
    MyCommentsService myCommentsService;

    @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @PostMapping("/pending")
    public IMOOCJSONResult myOrder(@ApiParam(name = "userId", value = "用户Id", required = true)
                                   @RequestParam String userId,
                                   @ApiParam(name = "orderId", value = "订单Id", required = true)
                                   @RequestParam String orderId) {
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.ok();
        }

        IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }
        Orders order = (Orders) checkResult.getData();
        if (order.getIsComment() == YesOrNo.YES.type) {
            return IMOOCJSONResult.errorMsg("该笔订单已经评价");
        }
        List<OrderItems> orderItems = myCommentsService.queryPendingComment(orderId);
        return IMOOCJSONResult.ok(orderItems);
    }

    @ApiOperation(value = "保存订单列表", notes = "保存订单列表", httpMethod = "POST")
    @PostMapping("/saveList")
    public IMOOCJSONResult saveList(@ApiParam(name = "userId", value = "用户Id", required = true)
                                    @RequestParam String userId,
                                    @ApiParam(name = "orderId", value = "订单Id", required = true)
                                    @RequestParam String orderId,
                                    @RequestBody List<OrderItemsCommentBO> commentList) {
        IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }
        // list 不能为空
        if (commentList == null || commentList.isEmpty() || commentList.size() == YesOrNo.NO.type) {
            return IMOOCJSONResult.errorMsg("评论内容不能为空");
        }
        myCommentsService.saveComments(userId, orderId, commentList);
        return IMOOCJSONResult.ok();
    }


    @ApiOperation(value = "查询我的评价", notes = "查询我的评价", httpMethod = "POST")
    @PostMapping("/query")
    public IMOOCJSONResult saveList(@ApiParam(name = "userId", value = "用户Id", required = true)
                                    @RequestParam String userId,
                                    @ApiParam(name = "page", value = "页码", required = false, defaultValue = "0")
                                    @RequestParam Integer page,
                                    @ApiParam(name = "pageSize", value = "显示条数", required = false, defaultValue = "10")
                                    @RequestParam Integer pageSize) {

        PageHelper.startPage(page, pageSize);
        List<MyCommentVO> myCommentVOList = myCommentsService.queryMyComments(userId);
        return IMOOCJSONResult.ok(getPageResult(myCommentVOList, page));
    }


}